<template>
  <div id="detail">
    <detail-nav-bar class="detail-bar" @titleClick="selectIndex" :current-index="currentIndex"></detail-nav-bar>
    <scroll class="content" ref="scroll"
            :probe-type="3"
            @scroll="contentScroll">
      <detail-swiper :top-images="topImages"/>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"></detail-goods-info>
      <detail-param-info :param-info="paramInfo" ref="param"></detail-param-info>
      <detail-comment-info :comment-info="commentInfo" ref="comment"></detail-comment-info>
      <goods-list ref="recommend" :goods="goodsList"></goods-list>
    </scroll>
    <!--    当我们监听组件的原生事件时，必须要添加native修饰符，才能监听。
            或者在组件里面定义事件后 发射出来也行-->
    <back-top @click.native="backTop" v-show="isShowBackTop"></back-top>
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
    <toast message="kkkkk"></toast>
  </div>
</template>

<script>
import DetailNavBar from "./childComps/DetailNavBar";
import Scroll from "components/common/scroll/Scroll";
import DetailSwiper from "./childComps/DetailSwiper";
import DetailBaseInfo from "./childComps/DetailBaseInfo";
import DetailShopInfo from "./childComps/DetailShopInfo";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
import DetailParamInfo from "./childComps/DetailParamInfo";
import DetailCommentInfo from "./childComps/DetailCommentInfo";
import GoodsList from "components/content/goods/GoodsList";
import DetailBottomBar from "./childComps/DetailBottomBar";
import Toast from "components/common/toast/Toast";

import {getDetail, Goods, Shop, GoodsParam, getRecommend} from "network/detail";
import {backTopMixin} from "common/mixin";
import {mapActions} from 'vuex'


export default {
  name: "Detail",
  components: {
    Toast,
    DetailBottomBar,
    GoodsList,
    DetailCommentInfo,
    DetailParamInfo,
    DetailGoodsInfo,
    DetailShopInfo,
    DetailBaseInfo,
    DetailSwiper,
    Scroll,
    DetailNavBar
  },
  mixins: [backTopMixin],
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      goodsList: [],
      themeTops: [],
      currentIndex: 0
    }
  },
  created() {
    // 1.保存传入的iid
    this.iid = this.$route.params.iid

    // 2.根据iid请求详情数据
    this._getDetail(this.iid);

    // 3.请求推荐请求
    this._getRecommend()
  },
  methods: {
    ...mapActions(['addCart']),
    imageLoad() {
      this.$refs.scroll.refresh()

      // 获取对应的offsetTop
      this.themeTops = []
      this.themeTops.push(0)
      this.themeTops.push(this.$refs.param.$el.offsetTop)
      this.themeTops.push(this.$refs.comment.$el.offsetTop)
      this.themeTops.push(this.$refs.recommend.$el.offsetTop)
      this.themeTops.push(Number.MAX_VALUE)
    },
    contentScroll(position) {
      // 1.判断BackTop是否显示
      // this.isShowBackTop = (-position.y) > BACK_POSITION
      this.listenShowBackTop(position);
      this.listenScrollTheme(-position.y);
    },
    listenScrollTheme(position) {
      let length = this.themeTops.length;
      for (let i = 0; i < length; i++) {
        let iPos = this.themeTops[i];

        if (position >= iPos && position < this.themeTops[i + 1]) {
          if (this.currentIndex !== i) {
            this.currentIndex = i;
          }
          break;
        }
      }
    },
    _getDetail(iid) {
      getDetail(iid).then(res => {
        // 1.获取数据
        const data = res.result;
        //2.获取顶部的图片轮播数据
        this.topImages = data.itemInfo.topImages

        // 3.获取商品信息
        this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
        // console.log(this.goods);

        // 4.创建店铺信息的对象
        this.shop = new Shop(data.shopInfo)
        // 5.取出的详情数据
        this.detailInfo = data.detailInfo;

        // 6.获取参数的信息
        this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)
        // console.log(this.paramInfo);

        //7.取出评论信息
        if (data.rate.cRate !== 0) {
          this.commentInfo = data.rate.list[0]
        }
      })
    },
    _getRecommend() {
      getRecommend().then(res => {
        this.goodsList = res.data.list
      })
    },
    selectIndex(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTops[index], 500)
    },
    addToCart() {
      //1.获取购物车需要展示的信息
      const product = {};
      product.iid = this.iid
      product.imgURL = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.desc
      product.price = this.goods.realPrice

      //2.将商品添加到购物车里
      // this.$store.dispatch('addCart', product).then(res => {
      //   console.log(res);
      // })
      //通过mapActions
      this.addCart(product).then((res)=>{
        this.$toast({message:'加入购物车成功'})
      })
    },
  }
}
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}

.detail-bar {
  position: relative;
  z-index: 9;
  background-color: #fff;
}

.content {
  background-color: #fff;
  height: calc(100% - 44px);
}
</style>
